package com.ets.cert.webapp.controller;

import com.ets.cert.service.CategoryManager;
import com.ets.cert.service.CertManager;
import com.ets.cert.dao.CertDao;
import com.ets.cert.model.Cert;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CertController extends BaseFormController{
	@Autowired
    private CertManager certManager;
	@Autowired
	private CertDao certDao;
	@Autowired
	private CategoryManager ccManager;
	
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(@RequestParam(required = false, value = "q") String query)
    throws Exception {
        return new ModelAndView().addObject(certManager.search(query, Cert.class));
    }
    
    @RequestMapping(value="/admin/cert/preAdd", method = RequestMethod.GET)
    public String preAdd(Model model) {
    	model.addAttribute("categoryList", ccManager.getAll());
    	return "/WEB-INF/pages/admin/addCert.jsp";
    }
    
    @ResponseBody
    @RequestMapping(value="/admin/cert/add", method = RequestMethod.POST)
    public String add(Cert cert) {
    	try {
    		certManager.save(cert);
    		return "SUCCESS";
    	} catch(Throwable t) {
    		return "FAIL: " + t.getMessage();
    	}
    }
    
    @ResponseBody
    @RequestMapping(value="/admin/cert/delete", method = RequestMethod.GET)
    public String delete(Cert cert, @RequestParam("id") Long id) {
    	try {
    		certManager.remove(id);
    		return "SUCCESS";
    	} catch(Throwable t) {
    		return "FAIL: " + t.getMessage();
    	}
    }
    
    @RequestMapping(value="/admin/cert/edit", method = RequestMethod.GET)
    public String edit(Model model, @RequestParam("id") Long id) {
    	model.addAttribute("cert", certManager.get(id));
    	return "/WEB-INF/pages/admin/editCert.jsp";
    }
    
    @RequestMapping(value="/admin/cert/list", method = RequestMethod.GET)
    public String certList(Model model) {
    	model.addAttribute("certList", certManager.getAll());
    	return "/WEB-INF/pages/admin/certList.jsp";
    }
    
    @RequestMapping(value="/cert/cert", method = RequestMethod.GET)
    public String certHome(Model model, @RequestParam(required=false, value="pid") Long pid,
    		@RequestParam(required=false, value="cid") Long cid) {
    	model.addAttribute("category", ccManager.getAll());
    	if (cid != null)
    		model.addAttribute("cid", cid);
    	if (pid != null)
    		model.addAttribute("pid", pid);
    	return "/WEB-INF/pages/public/cert.jsp";
    }
    
    @RequestMapping(value="/cert/certs")
    public String getCertById(Model model, @RequestParam(required=false, value="id") Long id) {
    	if (id != null) {
    		Cert c = certManager.get(id);
    		model.addAttribute("cert", c);
    	}
    	return "json";
    }
    
    @RequestMapping(value="/cert/getCertName")
    public String getCertByIds(Model model, @RequestParam(required=false, value="id") Long id) {
    	if (id != null) {
    		Cert c = certManager.get(id);
    		model.addAttribute("cert", c);
    	}
    	return "json";
    }
}
